package menu;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

class MenuItemListener implements ActionListener{
	
	JTextArea jtextarea;
	Clipboard clipboard;
	public MenuItemListener(JTextArea jtextarea, Clipboard clipboard)
	{
		this.jtextarea = jtextarea;
		this.clipboard = clipboard;
	}
	
	public void actionPerformed(ActionEvent actevent) {
		String cmd = actevent.getActionCommand();
		char choice = cmd.charAt(1);
		String text = "";
		switch(choice){
			case 'u':{
				text=jtextarea.getSelectedText();
				jtextarea.replaceRange("",jtextarea.getSelectionStart(),jtextarea.getSelectionEnd());					
				StringSelection stringselected = new StringSelection(text);
				clipboard.setContents(stringselected, null);
				break;
			}
			case 'o':{
				text=jtextarea.getSelectedText();
				StringSelection stringselected = new StringSelection(text);
				clipboard.setContents(stringselected, null);
				break;
			}
			case 'a':{
				
				Transferable transdata = clipboard.getContents(null);
				try {
					String pasteString = (String)transdata.getTransferData(DataFlavor.stringFlavor);
					jtextarea.replaceRange(pasteString,jtextarea.getSelectionStart(),jtextarea.getSelectionEnd());
				} catch (Exception e) {						
					JOptionPane.showMessageDialog(null,
						    e,
						    "Exception",
						    JOptionPane.ERROR_MESSAGE);
				} 
				break;
			}
			case 'x':{					
				System.exit(0);
				break;
			}
		}
	}
	
}